import { login, ldapAuth, getUserInfo } from '@/api/user'
import { setToken, getToken } from '@/libs/util'
import { Message } from 'iview'
// import { setUsername }  from '@/libs/util'

export default {
  state: {
    username: '',
    userId: '',
    avatorImgPath: '',
    token: getToken(),
    access: '',
    permission: '',
    hasGetInfo: false
  },
  mutations: {
    setAvator (state, avatorPath) {
      state.avatorImgPath = avatorPath
    },
    setUserId (state, id) {
      state.userId = id
    },
    setusername (state, name) {
      state.username = name
    },
    setAccess (state, access) {
      state.access = access
    },
    setToken (state, token) {
      state.token = token
      setToken(token)
    },
    // setPermissions (state, permission) {
    //   state.permission = permission
    //   setPermissions(permission)
    // },
    setHasGetInfo (state, status) {
      state.hasGetInfo = status
    }
  },
  actions: {
    // 登录
    handleLogin ({ commit }, {username, password, switchStatus}) {
      username = username.trim()
      return new Promise((resolve, reject) => {
        if (switchStatus === true) {
          ldapAuth({
            username,
            password
          }).then(res => {
            const data = res.data
            commit('setToken', data.token)
            resolve()
          }).catch(err => {
            reject(err)
            Message['error']({
              duration: 5,
              closable: true,
              background: true,
              content: '登陆失败！请检查用户名和密码是否正确。'
            })
          })
        } else {
          login({
            username,
            password
          }).then(res => {
            const data = res.data
            commit('setToken', data.token)
            resolve()
          }).catch(err => {
            reject(err)
            Message['error']({
              duration: 5,
              closable: true,
              background: true,
              content: '登陆失败！请检查用户名和密码是否正确。'
            })
          })
        }
      })
    },
    // 退出登录
    handleLogOut ({ state, commit }) {
      return new Promise((resolve, reject) => {
        // logout(state.token).then(() => {
        //   commit('setToken', '')
        //   commit('setAccess', [])
        //   resolve()
        // }).catch(err => {
        //   reject(err)
        // })
        // 如果你的退出登录无需请求接口，则可以直接使用下面三行代码而无需使用logout调用接口
        commit('setToken', '')
        // commit('setPermissions', '')
        commit('setAccess', [])
        sessionStorage.clear()
        resolve()
      })
    },
    // 获取用户相关信息
    getUserInfo ({ state, commit }) {
      return new Promise((resolve, reject) => {
        try {
          getUserInfo(state.token).then(res => {
            const data = res.data
            sessionStorage.setItem('username', data.username)
            sessionStorage.setItem('permission', JSON.stringify(data.permission))
            // setPermissions(JSON.stringify(data.permission))
            commit('setAvator', data.avator)
            commit('setusername', data.name)
            commit('setUserId', data.user_id)
            commit('setAccess', data.access)
            // commit('setPermissions', JSON.stringify(data.permission))
            commit('setHasGetInfo', true)
            resolve(data)
          }).catch(err => {
            reject(err)
          })
        } catch (error) {
          reject(error)
        }
      })
    }
  }
}
